package top.heliming.leyou.userconsumer.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import top.heliming.leyou.userconsumer.bean.User;
import top.heliming.leyou.userconsumer.dao.UserDao;
import top.heliming.leyou.userconsumer.service.UserFeignClient;
import top.heliming.leyou.userconsumer.service.UserService;

/**
 * description: //TODO
 *
 * @author: heliming
 * @date:2019/05/22 下午 10:47
 */
@Service
public class UserServiceImpl implements UserService {

  @Autowired
  private UserDao userDao;

  @Autowired
  private RestTemplate restTemplate;

  @Autowired
  private DiscoveryClient discoveryClient;// Eureka客户端，可以获取到服务实例信息

  @Autowired
  private UserFeignClient userFeignClient;//使用feign

/*
最原始直接用url访问
@Override
  public List<User> queryUserByIds(List<Long> ids){
    List<User> users = new ArrayList<>();
    for (Long id : ids) {
      User user = this.userDao.queryUserById(id);
      users.add(user);
    }
    return users;
  }*/

 /*
 用ip端口访问
 @Override
  public List<User> queryUserByIds(List<Long> ids) {
    List<User> users = new ArrayList<>();
    // String baseUrl = "http://localhost:8081/user/";
    // 根据服务名称，获取服务实例
    List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
    // 因为只有一个UserService,因此我们直接get(0)获取
    ServiceInstance instance = instances.get(0);
    // 获取ip和端口信息
    String baseUrl = "http://"+instance.getHost() + ":" + instance.getPort()+"/user/";
    ids.forEach(id -> {
      // 我们测试多次查询，
      users.add(this.restTemplate.getForObject(baseUrl + id, User.class));
      // 每次间隔500毫秒
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    });
    return users;
  }*/

  /*用服务名访问*/
 /* @Override
  public List<User> queryUserByIds(List<Long> ids) {
    List<User> users = new ArrayList<>();
    // 地址直接写服务名称即可
    String baseUrl = "http://user-service/user/";
    ids.forEach(id -> {
      // 我们测试多次查询，
      users.add(this.restTemplate.getForObject(baseUrl + id, User.class));
      // 每次间隔500毫秒
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    });
    return users;
  }*/

/*  *//**改造为熔断机制*//*
  @Override
  public List<User> queryUserByIds(List<Long> ids) {
    List<User> users = new ArrayList<>();
    ids.forEach(id -> {
      // 我们测试多次查询，
      users.add(this.userDao.queryUserById(id));
    });
    return users;
  }*/

/**改造为feign调用*/
  @Override
  public List<User> queryUserByIds(List<Long> ids) {
    List<User> users = new ArrayList<>();
    ids.forEach(id -> {
      // 我们测试多次查询，
      users.add(this.userFeignClient.queryUserById(id));
    });
    return users;
  }
}
